package com.yzh.wanandroid.component.fragment

import android.view.View
import com.google.android.material.tabs.TabLayoutMediator
import com.yzh.wanandroid.R
import com.yzh.wanandroid.adapter.ProjectArticleListAdapter
import com.yzh.wanandroid.adapter.PublicPagerAdapter
import com.yzh.wanandroid.adapter.WxArticListAdapter
import com.yzh.wanandroid.base.MvvmFragment
import com.yzh.wanandroid.component.activity.MainActivity
import com.yzh.wanandroid.databinding.FragmentPublicPagerBinding
import com.yzh.wanandroid.network.retrofit.responsebean.TitleData
import com.yzh.wanandroid.utils.SUCCESS
import com.yzh.wanandroid.vm.PublicPagerFragmentVm

class PublicPagerFragment : MvvmFragment<FragmentPublicPagerBinding, PublicPagerFragmentVm>() {

    override fun getLayoutId() = R.layout.fragment_public_pager

    override fun initViewModel(): PublicPagerFragmentVm = createViewModel()

    companion object {
        fun getInstance() = PublicPagerFragment()
    }

    override fun initData() {
        onLoadData()
    }

    override fun initView(view: View) {

    }

    private fun onLoadData() {
        mViewModel.titleData.observe(PublicPagerFragment@this) {
            if (it.loadingState == SUCCESS) {
                it.data?.data?.let { it1 ->
                    setViewPagerAdapterData(it1)
                    bindListener(it1)
                }
            }
        }
    }

    private fun setViewPagerAdapterData(data: List<TitleData>) {
        mBinding.wxArticlePager.run {
            this.adapter = PublicPagerAdapter(activity as MainActivity,data)
        }
    }

    private fun bindListener(data: List<TitleData>) {
        mBinding.run {
            TabLayoutMediator(wxArticleTitle, wxArticlePager, true) { tab, position ->
                tab.text = data[position].name
            }.attach()
        }
    }
}